# -*- coding: utf-8 -*-
from Agent.MainAgent import *
import Agent.MainAgent2
playboard = PlayBoard()

print(playboard.barrier_states)
print(playboard.reward_states)
if Config.SHOWN == True:
    playboard.drawingboard()
# env = deliveryManEnv(playboard.barrier_states, playboard.reward_states,
#                      play1state= playboard.state1, play2state = playboard.state2,
#                      play1score= playboard.play1score,play2score= playboard.play2score)
# node = Node(move = None,value = None,env=env,playturn = 2,deepth= 1,isTerminal = False,selfPlay = 1)
# action = alpha_beta_search(node)
reset = False

while(True):

    #play1走棋
    # boardSet = playboard.retunnLiBinBoard()
    # move = player1Greedy(boardSet, 1)
    # action = playboard.numActionDic[move]


    env = deliveryManEnv(playboard.barrier_states, reward_states = playboard.reward_states.copy(),
                         play1state=playboard.state1, play2state=playboard.state2,
                         play1score=playboard.play1score, play2score=playboard.play2score,
                         final1Score=playboard.final1Score,final2Score=playboard.final2Score,
                         play1jobs=playboard.play1capacity,play2jobs=playboard.play2capacity
                         )

    # aStar = AStar(startpoint=132, endpoint=env.play1base, env=env,player=1)
    # aStar.map2d.showarray2d()
    # aStar.action()
    # listaaa = env.getAllLegalMoveAroud(state = playboard.state1,player=1,deepth = 5)

    mainAgent = Maintrategy(env,player = 1)
    action = mainAgent.getAction(reset = reset)
    is_terminal = playboard.move(player=1, action=action)

    if Config.SHOWN == True:
        playboard.drawingboard()


    if is_terminal:
        playboard.terminal()
        playboard.reSet()
        reset = True
        Maintrategy.Runnum = 0
        Agent.MainAgent2.Maintrategy.Runnum = 0
        continue
    else:
        reset = False
    print("player 1 done")

    #player2走棋
    # env = deliveryManEnv(playboard.barrier_states, playboard.reward_states, playboard.state1, playboard.state2)
    # action = Q_learning_Agent(env, player = 2)

    env = deliveryManEnv(playboard.barrier_states, reward_states = playboard.reward_states.copy(),
                         play1state=playboard.state1, play2state=playboard.state2,
                         play1score=playboard.play1score, play2score=playboard.play2score,
                         final1Score=playboard.final1Score, final2Score=playboard.final2Score,
                         play1jobs=playboard.play1capacity,play2jobs=playboard.play2capacity
                         )
    # node = Node(move=None, value=None, env=env, playturn=1, deepth=1, isTerminal=False, selfPlay=2)
    #
    # action = alpha_beta_search(node)
    # is_terminal = playboard.move(player=2, action=action)
    mainAgent2 = Agent.MainAgent2.Maintrategy(env, player=2)
    action = mainAgent2.getAction(reset=reset)
    is_terminal = playboard.move(player=2, action=action)
    if Config.SHOWN == True:
        playboard.drawingboard()

    if is_terminal:
        playboard.terminal()
        playboard.reSet()
        reset = True
        Maintrategy.Runnum = 0
        Agent.MainAgent2.Maintrategy.Runnum = 0
        continue
    else:
        reset = False
    print("player 2 done")

